package com.nbufe.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nbufe.entity.CmsData;
import com.nbufe.service.CmsDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;

@Configuration
@EnableScheduling
public class CleanScheduleTask {

    @Autowired
    private CmsDataService cmsDataService;

    @Scheduled(fixedRate = 300000)
    private void configureTasks() {
        QueryWrapper<CmsData> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.le("createtime", LocalDateTime.now().plusHours(-24));
        objectQueryWrapper.orderByAsc("createTime");
        int count = cmsDataService.count(objectQueryWrapper);
        objectQueryWrapper.last("limit" + " " + (count - 100));
        objectQueryWrapper.orderByDesc("createTime");
        if (count > 10) {
            boolean s = cmsDataService.remove(objectQueryWrapper);
            System.out.println(s);
        }
    }
}
